package com.shaw.workrecord.controller;

import com.shaw.workrecord.entity.PageResponse;
import com.shaw.workrecord.entity.ResponseBean;
import com.shaw.workrecord.pojo.request.RecordAddRequest;
import com.shaw.workrecord.pojo.request.RecordEditRequest;
import com.shaw.workrecord.pojo.request.RecordPageRequest;
import com.shaw.workrecord.pojo.response.RecordResponse;
import com.shaw.workrecord.service.IRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(tags="账单记录模块")
@Validated
public class RecordController {

    @Autowired
    private IRecordService service;

    @GetMapping("/record")
    @ApiOperation("账单分页列表")
    public ResponseBean<PageResponse<RecordResponse>> getPage(RecordPageRequest request) {
        return ResponseBean.success(service.getRecordPage(request));
    }

    @PostMapping("/record")
    @ApiOperation("新增账单")
    public ResponseBean<Boolean> addRecord(@RequestBody RecordAddRequest request) {
        if (service.addRecord(request)) {
            return ResponseBean.success(true);
        }
        return ResponseBean.failure("新增失败");
    }

    @PutMapping("/record")
    @ApiOperation("编辑账单")
    public ResponseBean<Boolean> editRecord(@RequestBody RecordEditRequest request) {
        if (service.editRecord(request)) {
            return ResponseBean.success(true);
        }
        return ResponseBean.failure("编辑失败");
    }

    @DeleteMapping("/record/{id}")
    @ApiOperation("删除账单")
    public ResponseBean<Boolean> deleteRecord(@PathVariable("id") Integer id) {
        if (service.deleteRecord(id)) {
            return ResponseBean.success(true);
        }
        return ResponseBean.failure("删除失败");
    }

    @GetMapping("/record/detail/{id}")
    @ApiOperation("账单详情")
    public ResponseBean<RecordResponse> getRecordDetail(@PathVariable("id") Integer id){
        return ResponseBean.success(service.getRecordDetail(id));
    }
}
